<?
include '../includes/specs_functions.php';

#----------------------------------------------------
# Connect to PADS
$DB = getDB();
#-------------------------------$Session information
$SESSION = get_session($DB);
$CICUID = $SESSION->Character;

$action = $_POST['action'];

$table = SPEC_TBL_JOBS." as job";
$col = array(
		"job.".SPEC_TBL_JOBS_ID." as id"
		,"job.".SPEC_TBL_JOBS_NAME." as name"	
		,"job.".SPEC_TBL_JOBS_TRI." as tri"			
		,"job.".SPEC_TBL_JOBS_TYPE." as type"	
		);
$orderBy = "job.".SPEC_TBL_JOBS_NAME;
$direction = "ASC";

$result = $DB->GetData($table ,$col ,true ,null, $orderBy, $direction);

$levels = array();

if($action == 'reset'){
	
	//Reset to 50
	foreach ($result[3] as $row) {
		$level = array( 
						"id" => $row['id']
						,"level" => 50
					);
		array_push($levels, $level);
	}
}else{
	//get job levels
	$options = array( 'http' => array(
		'user_agent'    => 'crawler',                                // Tell Lodestone we are an outside website.
		'max_redirects' => 10,        							    // Set maximum redirects allowed.
		'timeout'       => 120,										// Set a timeout limit just in case.
		'header'        => 'Accept-language: en-us,en;q=0.5',       // Tell Lodestone to output English instead of Japanese.
	) );	
	$context = stream_context_create( $options );
	$charUrl = "http://lodestone.finalfantasyxiv.com/rc/character/status?cicuid=$CICUID";
	$charInput = file_get_contents($charUrl, false, $context);
	foreach ($result[3] as $row) {
		$level = array(
						"id" => $row['id']
						,"level" => findString("id=\"".strtolower($row['tri'])."\"", "class=\"contents-table1TD", "<b>", "</b>", $charInput)
					);
		array_push($levels, $level);
	}
	
}
	
echo json_encode($levels);
	
#------------------------------------------------------------------------
# Disconnect
leaveDB($DB);


?>